<?php
class Admin_TinTucController extends Zend_Controller_Action
{

    public function init()
    {
          $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
    	$tintucMapper = Library_Model_TinTucMapper::getInstance();
    	$tintuc = $tintucMapper->baivietList();
    	$this->view->tintuc = $tintuc;
    	
    	Zend_Registry::get('logger')->info($tintuc);
    }

    public function themAction()
    {
    	$this->view->title = 'Thêm tin tức';
    	$formThem = new Admin_Form_TinTucForm();
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/tintuc/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	$formThem->getElement('tenbaiviet')->addValidator('Db_NoRecordExists',true, array(
    			'table' => 'tbl_tintuc',
    			'field' => 'TenBaiViet'
    	));
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$machude 		= $data['machude'];
    			$mand 			= $data['mand'];
    			$tenbaiviet		= $data['tenbaiviet'];
    			$noidung 			= $data['noidung'];
    			//$ngaydang		= $data['ngaydang'];
    			$luotxem			= $data['luotxem'];
				$kiemduyet		= $data['kiemduyet'];
				$tomtat				= $data['tomtat'];
			
    			$tintucMapper = Library_Model_TinTucMapper::getInstance();
    			$tintucObj 	= new Library_Model_TinTuc();
    			    			
    			$tintucObj->setMachude($machude);
    			$tintucObj->setMand($mand);
    			$tintucObj->setTenbaiviet($tenbaiviet);
    			$tintucObj->setNoidung($noidung);
				$tintucObj->setNgaydang(Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'));
				$tintucObj->setLuotxem($luotxem);
				$tintucObj->setKiemduyet($kiemduyet);
				$tintucObj->setTomtat($tomtat);
				
    	
    			$tintuc = $tintucMapper->save($tintucObj);
    			$this->_redirect('/admin/tintuc/index');
    		}
    	}
    }

    public function capnhatAction()
    {
        $this->view->title = 'Cập nhật tin tức';
        $formCapnhat = new Admin_Form_TinTucForm();        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/tintuc/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $mabaiviet = $this->getRequest()->getParam('id');
        
        
        $tintucMapper = Library_Model_TinTucMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 					= $data['id'];
        		$machude 		= $data['machude'];
    			$mand 			= $data['mand'];
    			$tenbaiviet		= $data['tenbaiviet'];
    			$noidung 			= $data['noidung'];
    			//$ngaydang		= $data['ngaydang'];
    			$luotxem			= $data['luotxem'];
				$kiemduyet		= $data['kiemduyet'];
				$tomtat				= $data['tomtat'];
			
    			
    			$tintucObj 	= new Library_Model_TinTuc();

    			$tintucObj->setId($id);
    			$tintucObj->setMachude($machude);
    			$tintucObj->setMand($mand);
    			$tintucObj->setTenbaiviet($tenbaiviet);
    			$tintucObj->setNoidung($noidung);
				$tintucObj->setNgaydang(Zend_Date::now()->get('yyyy-MM-dd HH:mm:ss'));
				$tintucObj->setLuotxem($luotxem);
				$tintucObj->setKiemduyet($kiemduyet);
				$tintucObj->setTomtat($tomtat);
				
    	
    			$tintuc = $tintucMapper->save($tintucObj);
    			$this->_redirect('/admin/tintuc/index');
        	}
        } else {
        		$tintuc = $tintucMapper->searchTinTucById($mabaiviet);
        		
        		$formCapnhat->getElement('id')->setValue($tintuc->getId());
        		$formCapnhat->getElement('machude')->setValue($tintuc->getMachude());        		
        		$formCapnhat->getElement('mand')->setValue($tintuc->getMand());
        		$formCapnhat->getElement('tenbaiviet')->setValue($tintuc->getTenbaiviet());
        		$formCapnhat->getElement('noidung')->setValue($tintuc->getNoidung());
				//$formCapnhat->getElement('ngaydang')->setValue($tintuc->getNgaydang());
				$formCapnhat->getElement('luotxem')->setValue($tintuc->getLuotxem());
				$formCapnhat->getElement('kiemduyet')->setValue($tintuc->getKiemduyet());
				$formCapnhat->getElement('tomtat')->setValue($tintuc->getTomtat());
			}
        
    }

    public function xemAction(){
    	$tt_id = $this->_request->getParam('id');
    	
    	$tintucMapper = Library_Model_TinTucMapper::getInstance();
    	$tintuc = $tintucMapper->searchTinTucById($tt_id);
    	$this->view->tintuc = $tintuc;
    	
    }
    public function xoaAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        $mabaiviet = $this->getRequest()->getParam('id');
        $tintucMapper = Library_Model_TinTucMapper::getInstance();
        $tintucMapper->delete($mabaiviet);
        $this->_redirect('/admin/tintuc/index');
    }
    
}









		
		
		
		
		
		
		